package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBean;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;


@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;

/*
    @Override
    public PageBean page(Integer page, Integer pageSize) {
//        1.获取总记录数
        Long count = empMapper.count();
//        2.获取分页查询结果列表
        List<Emp> empList = empMapper.page((page-1) * pageSize, pageSize);
//        3.封装PageBean对象
        return new PageBean(count,empList);
    }
*/

    @Override
    public PageBean page(Integer page, Integer pageSize, String name, Short gender, LocalDate begin, LocalDate end) {
        // 1.设置分页参数
         PageHelper.startPage(page, pageSize);

        // 2.执行查询  PageHelper 会自动返回 Page 类型的结果
        List<Emp> empList = empMapper.list(name, gender, begin, end);

        // 3. 获取分页信息
        Page<Emp> p = (Page<Emp>) empList;  // 使用 Page 类型的对象

        // 4. 封装 PageBean 对象
        return new PageBean(p.getTotal(), p.getResult());
    }

    @Override
    public void delete(List<Integer> ids) {
        empMapper.delete(ids);
    }

    @Override
    public void insert(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.insert(emp);
    }

    @Override
    public Emp findById(Integer id) {
        return empMapper.findById(id);
    }

    @Override
    public void update(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.update(emp);

    }

    @Override
    public Emp login(Emp emp) {
        return empMapper.getByUsernameAndPassword(emp);
    }


}
